
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/bean/lot_entity.dart';
import 'package:xin_nuo_pda/values/constant.dart';
import 'package:xin_nuo_pda/views/SubTitleBar.dart';
import 'package:xin_nuo_pda/views/TextView.dart';
import 'package:xin_nuo_pda/views/edittext/TitleEditText.dart';
import 'package:xin_nuo_pda/utils/string_extension.dart';
import 'package:xin_nuo_pda/values/colors.dart';
import 'TestLotStartWorkController.dart';

class TestLotStartWorkPage extends StatelessWidget {
  final controller = Get.put(TestLotStartWorkController());
  final state = Get.find<TestLotStartWorkController>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(USER),
        ),
        body: SingleChildScrollView(
          child: GetBuilder<TestLotStartWorkController>(
            builder: (controller) => Container(
              child: Column(
                children: [
                  SubTitleBar("测试进站", '提交', () {
                      controller.trackIn();
                  }),
                  TitleEditText(
                    "批次ID:",
                    "请扫描批次ID",
                    (value) {
                      controller.getLotInfo(value);
                      state.focusNode1.unfocus();
                      FocusScope.of(Get.context!).requestFocus(state.focusNode2);
                    },
                    focusNode: state.focusNode1,
                    controller: state.controller1,
                  ),
                  GetBuilder<TestLotStartWorkController>(
                    builder: (controller) => Column(
                      children: [
                        TextView("当前工序:", state.lotEntity?.sTEPDESC ?? ''),
                        TextView("数量:", state.lotEntity?.mAINQTY.toString() ?? ''),
                      ],
                    ),
                    id: state.lotInfoID,
                  ),
                  TitleEditText(
                    "设备ID:",
                    "请扫描设备ID",
                    (value) {
                      // state.focusNode2.unfocus();
                      // FocusScope.of(Get.context!).requestFocus(state.focusNode3);
                    },
                    focusNode: state.focusNode2,
                    controller: state.controller2,
                  ),
                  // TitleEditText(
                  //   "设备ID(测试机):",
                  //   "请扫描设备ID(测试机)",
                  //   (value) {
                  //     state.focusNode3.unfocus();
                  //     FocusScope.of(Get.context!).requestFocus(state.focusNode4);
                  //   },
                  //   focusNode: state.focusNode3,
                  //   controller: state.controller3,
                  // ),
                  // TitleEditText(
                  //   "设备ID(打标机):",
                  //   "请扫描设备ID(打标机)",
                  //   (value) {},
                  //   focusNode: state.focusNode4,
                  //   controller: state.controller4,
                  // ),
                  Column(
                    children: [
                      Container(
                        margin: EdgeInsets.all(8),
                        alignment: Alignment.centerLeft,
                        child: Text(
                          "主物料BOM信息",
                          style: TextStyle(
                              fontWeight: FontWeight.bold, fontSize: 16),
                        ),
                      ),
                      GetBuilder<TestLotStartWorkController>(
                        builder: (controller) => Table(
                          defaultVerticalAlignment:
                              TableCellVerticalAlignment.middle,
                          columnWidths: {
                            //列宽
                            0: FlexColumnWidth(2.0),
                            1: FlexColumnWidth(1.0),
                            2: FlexColumnWidth(1.0),
                            3: FlexColumnWidth(2.0),
                          },
                          border: TableBorder.all(
                            color: Colors.grey,
                            width: 1.0,
                            style: BorderStyle.solid,
                          ),
                          children: getTableList(context),
                        ),
                        id: state.materialInfoID,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          InkWell(
                            onTap: (){
                                controller.addTempMaterial();
                            },
                            splashColor: Colors.transparent,
                            child: Container(
                              width: 50.0,
                              height: 30.0,
                              alignment: Alignment.center,
                              margin: const EdgeInsets.fromLTRB(12, 8, 12, 8),
                              decoration: BoxDecoration(
                                  color: Colors.blueAccent,
                                  borderRadius: BorderRadius.circular(50),
                                  border: Border.all(color: Colors.blueAccent,width: 1)
                              ),
                              child: Text(
                                '增加',
                                style: TextStyle(
                                    color: Colors.white),
                              ),
                            ),
                          ),
                          InkWell(
                            onTap: (){
                              controller.removeTempMaterial();
                            },
                            splashColor: Colors.transparent,
                            child: Container(
                              width: 50.0,
                              height: 30.0,
                              alignment: Alignment.center,
                              margin: const EdgeInsets.fromLTRB(12, 8, 12, 8),
                              decoration: BoxDecoration(
                                  color: Colors.red,
                                  borderRadius: BorderRadius.circular(50),
                                  border: Border.all(color: Colors.red,width: 1)
                              ),
                              child: Text(
                                '移除',
                                style: TextStyle(
                                    color: Colors.white),
                              ),
                            ),
                          ),
                        ],
                      )
                    ],
                  ),
                  Column(
                    children: [
                      Container(
                        margin: EdgeInsets.all(8),
                        alignment: Alignment.centerLeft,
                        child: Text(
                          "关键物料BOM信息",
                          style: TextStyle(
                              fontWeight: FontWeight.bold, fontSize: 16),
                        ),
                      ),
                      GetBuilder<TestLotStartWorkController>(
                        builder: (controller) => Table(
                          defaultVerticalAlignment:
                          TableCellVerticalAlignment.middle,
                          columnWidths: {
                            //列宽
                            0: FlexColumnWidth(2.0),
                            1: FlexColumnWidth(1.0),
                            2: FlexColumnWidth(1.0),
                            3: FlexColumnWidth(2.0),
                          },
                          border: TableBorder.all(
                            color: Colors.grey,
                            width: 1.0,
                            style: BorderStyle.solid,
                          ),
                          children: getCureTableList(context),
                        ),
                        id: state.materialInfoID,
                      ),
                    ],
                  ),
                  TitleEditText(
                    "半成品物料投入数:",
                    "",
                        (value) {
                    },
                    isScan: false,
                    type: TextInputType.number,
                    controller: state.controller5,
                  ),
                ],
              ),
            ),
          ),
        ));
  }

  List<TableRow> getTableList(BuildContext context) {
    List<TableRow> tempView = [];
    List<FocusNode> focusNodeList = [];
    tempView.add(TableRow(
        children: [
          //增加行高
          SizedBox(
            height: 40,
            child: Align(
              alignment: Alignment.center,
              child: Text(
                '物料料号',
                style: TextStyle(
                  color: theme_blue.toColor(),
                ),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '物料类型',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '单位',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '物料批号',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
        ],
        //第一行样式 添加背景色
        decoration: BoxDecoration(
          color: table_head.toColor(),
        )));
    for (int i = 0; i < (state.lotEntity?.MAINBOMLINELIST??[]).length; i++) {
      LotBOMLINELIST element = (state.lotEntity?.MAINBOMLINELIST??[])[i];
      tempView.add(TableRow(children: [
        //增加行高
        SizedBox(
          height: 40,
          child: Align(
            alignment: Alignment.center,
            child: Text(
              element.mATERIALNAME ?? '',
              style: TextStyle(
                color: text_black.toColor(),
              ),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.mATERIALTYPE ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.uOMID ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.topCenter,
          child: Container(
            padding: EdgeInsets.only(bottom: 4),
            child:
            TextField(
              decoration: InputDecoration(
                border: new UnderlineInputBorder(
                    borderSide: new BorderSide(color: Colors.red)),
                hintText: '请扫描验证',
                hintStyle: TextStyle(
                  fontSize: 14,
                ),
              ),
              onChanged: (value) {
                if (i < (state.lotEntity?.bOMLINELIST??[]).length - 1) {
                  focusNodeList[i].unfocus();
                  FocusScope.of(Get.context!).requestFocus(focusNodeList[i + 1]);
                }
                element.MLOTID = value;
              },
              controller: TextEditingController(text: element.MLOTID),
            )
          ),
        ),
      ]));
    }
    return tempView;
  }

  List<TableRow> getCureTableList(BuildContext context) {
    List<TableRow> tempView = [];
    List<FocusNode> focusNodeList = [];
    tempView.add(TableRow(
        children: [
          //增加行高
          SizedBox(
            height: 40,
            child: Align(
              alignment: Alignment.center,
              child: Text(
                '物料料号',
                style: TextStyle(
                  color: theme_blue.toColor(),
                ),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '物料类型',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '单位',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '物料批号',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
        ],
        //第一行样式 添加背景色
        decoration: BoxDecoration(
          color: table_head.toColor(),
        )));
    for (int i = 0; i < (state.lotEntity?.bOMLINELIST??[]).length; i++) {
      LotBOMLINELIST element = (state.lotEntity?.bOMLINELIST??[])[i];
      tempView.add(TableRow(children: [
        //增加行高
        SizedBox(
          height: 40,
          child: Align(
            alignment: Alignment.center,
            child: Text(
              element.mATERIALNAME ?? '',
              style: TextStyle(
                color: text_black.toColor(),
              ),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.mATERIALTYPE ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.uOMID ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.topCenter,
          child: Container(
            padding: EdgeInsets.only(bottom: 4),
            child: TextField(
              decoration: InputDecoration(
                border: new UnderlineInputBorder(
                    borderSide: new BorderSide(color: Colors.red)),
                hintText: '请扫描验证',
                hintStyle: TextStyle(
                  fontSize: 14,
                ),
              ),
              onChanged: (value) {
                if (i < (state.lotEntity?.bOMLINELIST??[]).length - 1) {
                  focusNodeList[i].unfocus();
                  FocusScope.of(Get.context!).requestFocus(focusNodeList[i + 1]);
                }
                element.MLOTID = value;
              },
              controller: TextEditingController(text: element.MLOTID),
            )
          ),
        ),
      ]));
    }
    return tempView;
  }
}
